package com.qinggeng.project.system.controller;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.SysLeague;
import com.qinggeng.project.system.service.ISysLeagueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 加盟信息Controller
 *
 * @author qinggeng
 * @date 2020-05-28
 */
@RestController
@RequestMapping("/system/league")
public class SysLeagueController extends BaseController
{
    @Autowired
    private ISysLeagueService sysLeagueService;

    /**
     * 查询加盟信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:league:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLeague sysLeague)
    {
        startPage();
        List<SysLeague> list = sysLeagueService.selectSysLeagueList(sysLeague);
        return getDataTable(list);
    }

    /**
     * 导出加盟信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:league:export')")
    @Log(title = "加盟信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysLeague sysLeague)
    {
        List<SysLeague> list = sysLeagueService.selectSysLeagueList(sysLeague);
        ExcelUtil<SysLeague> util = new ExcelUtil<SysLeague>(SysLeague.class);
        return util.exportExcel(list, "league");
    }

    /**
     * 获取加盟信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:league:query')")
    @GetMapping(value = "/{leagueId}")
    public AjaxResult getInfo(@PathVariable("leagueId") Long leagueId)
    {
        return AjaxResult.success(sysLeagueService.selectSysLeagueById(leagueId));
    }

    /**
     * 新增加盟信息
     */
    @PreAuthorize("@ss.hasPermi('system:league:add')")
    @Log(title = "加盟信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysLeague sysLeague)
    {
        return toAjax(sysLeagueService.insertSysLeague(sysLeague));
    }

    /**
     * 修改加盟信息
     */
    @PreAuthorize("@ss.hasPermi('system:league:edit')")
    @Log(title = "加盟信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysLeague sysLeague)
    {
        return toAjax(sysLeagueService.updateSysLeague(sysLeague));
    }

    /**
     * 处理加盟信息
     */
    @PreAuthorize("@ss.hasPermi('system:league:edit')")
    @Log(title = "加盟信息", businessType = BusinessType.UPDATE)
    @PostMapping("/deal")
    public AjaxResult deal(@RequestBody SysLeague sysLeague)    {
        if(!sysLeague.getStatus().equals(CommonEnum.STATUS_10.getValue())){
            return AjaxResult.error("该加盟信息已被处理过，请勿重复处理！", ShowTypeEnum.用户.getValue());
        }
        sysLeague.setStatus(CommonEnum.STATUS_20.getValue());
        return toAjax(sysLeagueService.updateSysLeague(sysLeague));
    }

    /**
     * 删除加盟信息
     */
    @PreAuthorize("@ss.hasPermi('system:league:remove')")
    @Log(title = "加盟信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{leagueIds}")
    public AjaxResult remove(@PathVariable Long[] leagueIds)
    {
        return toAjax(sysLeagueService.deleteSysLeagueByIds(leagueIds));
    }
}
